
#ifndef __SYSLIST_H
#define __SYSLIST_H



#include "main.h"
#include <stdbool.h>
#include "bsp.h"
#include "SysMemMan.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */

/**
 * @ingroup los_list
 * Structure of a node in a doubly linked list.
 */
typedef struct SYS_LIST {
    struct SYS_LIST *pstPrev; /**< Current node's pointer to the previous node */
    struct SYS_LIST *pstNext; /**< Current node's pointer to the next node */
} SYS_LIST;


inline  void Sys_ListInit(SYS_LIST *list)
{
    list->pstNext = list;
    list->pstPrev = list;
}

#define SYS_LIST_FIRST(object) ((object)->pstNext)


inline void Sys_ListAdd(SYS_LIST *list, SYS_LIST *node)
{
    node->pstNext = list->pstNext;
    node->pstPrev = list;
    list->pstNext->pstPrev = node;
    list->pstNext = node;
}


inline void Sys_ListTailInsert(SYS_LIST *list, SYS_LIST *node)
{
    Sys_ListAdd(list->pstPrev, node);
}

inline void Sys_ListHeadInsert(SYS_LIST *list, SYS_LIST *node)
{
    Sys_ListAdd(list, node);
}

inline void Sys_ListDelete(SYS_LIST *node)
{
    node->pstNext->pstPrev = node->pstPrev;
    node->pstPrev->pstNext = node->pstNext;
    node->pstNext = (SYS_LIST *)NULL;
    node->pstPrev = (SYS_LIST *)NULL;
}

inline bool LOS_ListEmpty(SYS_LIST *node)
{
    return (bool)(node->pstNext == node);
}


#define SYS_OFF_SET_OF(type, member) ((uint32_t)&(((type *)0)->member)) /*lint -e(413) */


#define SYS_LIST_ENTRY(item, type, member) \
    ((type *)(void *)((char *)(item) - SYS_OFF_SET_OF(type, member))) \


#define SYS_LIST_FOR_EACH_ENTRY(item, list, type, member) \
    for ((item) = SYS_LIST_ENTRY((list)->pstNext, type, member); \
            &(item)->member != (list); \
            (item) = SYS_LIST_ENTRY((item)->member.pstNext, type, member))


#define SYS_LIST_FOR_EACH_ENTRY_SAFE(item, next, list, type, member) \
    for ((item) = SYS_LIST_ENTRY((list)->pstNext, type, member), \
            (next) = SYS_LIST_ENTRY((item)->member.pstNext, type, member); \
            &((item)->member) != (list); \
            (item) = (next), (next) = SYS_LIST_ENTRY((item)->member.pstNext, type, member))


inline void Sys_ListDelInit(SYS_LIST *list)
{
    list->pstNext->pstPrev = list->pstPrev;
    list->pstPrev->pstNext = list->pstNext;
    Sys_ListInit(list);
}

#define SYS_LIST_FOR_EACH(item, list) \
    for ((item) = (list)->pstNext; (item) != (list); (item) = (item)->pstNext)

#define LOS_DL_LIST_FOR_EACH_SAFE(item, next, list) \
    for ((item) = (list)->pstNext, (next) = (item)->pstNext; (item) != (list); \
            (item) = (next), (next) = (item)->pstNext)


#define LOS_DL_LIST_HEAD(list) \
    SYS_LIST list = { &(list), &(list) }

	
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */

#endif /* _LOS_LIST_H */
